package com.hong.alert;

import models.BufferSize;
import models.EventsType;
import models.GenerationRate;
import models.LogQueueSize;
import models.RealRate;
import models.SubscribeEvents;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import system.main.Main;

@Controller
public class StartController {

	// private SystemDataStructure ds;
	private EventsType eventsType;
	private RealRate realRate;
	private BufferSize bufferSize;
	private SubscribeEvents subscribeEvents;
	private LogQueueSize logQueueSize;
	private Main main;

	@RequestMapping(value = "/display", method = RequestMethod.POST)
	public ModelAndView display(GenerationRate generationRate, Model model) {
		// ds = new SystemDataStructure();
		// scheduler = new Scheduler(ds);
		// notifyer = new Notifier(ds);
		// realRate = new RealRate();
		// eventsType = ds.getEventType();
		// bufferSize = new BufferSize();
		// subscribeEvents = ds.getSubscribeEvents();
		// logQueueSize = new LogQueueSize();
		//
		// ds.setGenerationRate(generationRate);
		// notifyer.start();
		// scheduler.start();
		//
		// for (int i = 0; i < 1; i++) {
		// new EventsGenerator(ds).start();
		// }
		//
		// new Logger(ds).start();
		main = new Main(generationRate);
		bufferSize = main.getBufferSize();
		eventsType = main.getEventsType();
		realRate = main.getRealRate();
		subscribeEvents = main.getSubscribeEvents();
		logQueueSize = main.getLogQueueSize();

		model.addAttribute(bufferSize);
		model.addAttribute(eventsType);
		model.addAttribute(realRate);
		model.addAttribute(subscribeEvents);
		model.addAttribute(logQueueSize);
		return new ModelAndView("display");

	}

	@RequestMapping(value = "/display", method = RequestMethod.GET)
	public ModelAndView refresh(GenerationRate generationRate, Model model) {

		bufferSize = main.getBufferSize();
		eventsType = main.getEventsType();
		// realRate = main.getRealRate();
		subscribeEvents = main.getSubscribeEvents();
		logQueueSize = main.getLogQueueSize();

		double realNormal = (double) eventsType.getNormal();
		double realWarning = (double) eventsType.getWarning();
		double realAlert = (double) eventsType.getAlert();
		double total = realNormal + realWarning + realAlert;
		realRate.setNormalRate(realNormal / total * 100);
		realRate.setWarningRate(realWarning / total * 100);
		realRate.setAlertRate(realAlert / total * 100);

		// bufferSize.setSize(ds.getEventBuffer().getSize());
		// logQueueSize.setSize(ds.getLogQueue().getSize());

		model.addAttribute(bufferSize);
		model.addAttribute(realRate);
		model.addAttribute(eventsType);
		model.addAttribute(subscribeEvents);
		model.addAttribute(logQueueSize);
		return new ModelAndView("display");

	}
}
